<?xml version="1.0" encoding="utf-8"?>

<!--Author: Piyush Garg-->
<!--Student ID: S3218012-->
<!--Assignment2: Document Markup Language-->

<xsl:stylesheet version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="xsi">
	<xsl:output method="xml" indent="yes"/>
	
	<xsl:variable name="smallletters" select="'abcdefghijklmnopqrstuvwxyz'" />
	<xsl:variable name="capitalletters" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
	
    <xsl:template match="/">

		<!--Root element of the XML File with the attribute and its value-->
		<xsl:element name="Dictionary" >
			<xsl:attribute name="xsi:noNamespaceSchemaLocation">
				<xsl:value-of select="'translation.xsd'"/>
			</xsl:attribute>

			<!--Child element of Dictionary of an XML File-->
	
			<!--from element-->
			<xsl:element name="from">
				<xsl:value-of select="translate(wordlist/@from,$smallletters,$capitalletters)"/>
			</xsl:element>
	
			<!--to element-->
			<xsl:element name="to">
				<xsl:value-of select="translate(wordlist/@to,$smallletters,$capitalletters)"/>
			</xsl:element>
	
			<!--total element-->
			<xsl:element name="total">
				<xsl:value-of select="count(//search[not(.=preceding::search)])"/>
			</xsl:element>
			
			<!--The xsl:for-each-group  instruction allocates the items in an input sequence into groups of items (that is, it establishes a 		  
			collection of sequences) based either on common values of a grouping key, or on a pattern that the initial or final node in a group must 
			match.] -->
			<xsl:for-each-group select="wordlist/wordEntry" group-by="search">
				
				<xsl:sort select="search" />
				
				<!--The evaluation context for XPath expressions includes a component called the current grouping key, which is an atomic value. The 
				current grouping key is the grouping key shared in common by all the items within the current group.]-->
				<xsl:variable name="searchlist" select="current-grouping-key()" />
				
				<!--translation element for Dictionary (Contains all the searched and replaces and counter of each search-->
				<xsl:element name="translation" >
					<!--Attribute initial with the starting letter of the word-->
					<xsl:attribute name="initial">
						<xsl:value-of select="substring(search,1,1)" />
					</xsl:attribute>
					
					<!--Another element of translation element "search" contains the names on the counts of Book-->
					<xsl:element name="search">
						<xsl:value-of select="$searchlist" />
					</xsl:element>
					
					<!--Another element of translation element "counter" contains the count of the replaces of the Books-->
					<!--The evaluation context for XPath expressions  includes a component called the current group, which is a sequence. The 	
					current group is the collection of related items that are processed collectively in one iteration of the xsl:for-each-group  
					element.]-->
					<xsl:element name="counter">
						<xsl:value-of select="count(current-group()/replace[not(.=preceding::replace[../search=$searchlist])])"/>
					</xsl:element>
					
					<!--Another element of translation element "replace" contains the replaces on the Names of Book-->
					<!--for-each element of xslt to get the current value of the replace tag and for replace tag create the seperate <replace> tag-->
					<xsl:for-each select="current-group()/replace[not(.=preceding::replace[../search=$searchlist])]">
						<xsl:element name="replace">
							<xsl:value-of select="." />
						</xsl:element>
					</xsl:for-each>
				
				<!--End of translation element-->
				</xsl:element>
			<!--End of for-each-group tag-->
			</xsl:for-each-group>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>

	

